package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.UserVisualBase;
import com.ruoyi.system.service.UserVisualBaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author tang ao long
 * @Date 2020-08-18 10:56
 * @Version 1.0
 */
@Controller
@RequestMapping("/system/UserVisualBase")
public class SysUserVisualBaseController extends BaseController {

    @Autowired
    private UserVisualBaseService userVisualBaseService;

    private String prefix = "system/uservisualbase";

    @RequiresPermissions("system:userVisualBase:view")
    @GetMapping()
    public String userVisualBase()
    {
        return prefix + "/userVisualBase";
    }

    @RequiresPermissions("system:userVisualBase:view")
    @GetMapping("/{groupId}")
    public String userVisualBaseGroup(@PathVariable("groupId") String groupId,ModelMap modelMap)
    {
        modelMap.put("groupId",groupId);
        return prefix + "/userVisualBase";
    }

    /**
     * 查询用户大屏信息
     * @param userVisualBase 查询对象
     * @return 返回查询结果
     */
    @RequiresPermissions("system:userVisualBase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserVisualBase userVisualBase)
    {
        startPage();
        List<UserVisualBase> userVisualBaseList = userVisualBaseService.selectUserVisualBaseListByInfo(userVisualBase);
        return getDataTable(userVisualBaseList);
    }

    /**
     * 删除大屏信息
     * @param ids 待删除的ID列
     * @return 返回删除结果
     */
    @RequiresPermissions("system:userVisualBase:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userVisualBaseService.deleteUserVisualBaseByIds(ids));
    }

    /**
     * 修改大屏信息
     */
    @RequiresPermissions("system:userVisualBase:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap map)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        map.put("user",user);
        map.put("userVisualBase", userVisualBaseService.selectUserVisualBaseListById(id));
        return prefix + "/edit";
    }

    /**
     * 大屏信息更新
     */
    @RequiresPermissions("system:userVisualBase:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated UserVisualBase userVisualBase,
                           @RequestParam("file") MultipartFile file) throws IOException
    {

        if(!file.isEmpty()){
            System.out.println(file.getName());
            //文件上传
            String fileName = FileUploadUtils.upload(file);
            userVisualBase.setPicUrl(fileName.substring(1));
        }

        userVisualBase.setUpdateId(ShiroUtils.getSysUser().getUserId().toString());
        userVisualBase.setUpdateName(ShiroUtils.getSysUser().getLoginName());
        return toAjax(userVisualBaseService.updateUserVisualBaseById(userVisualBase));
    }

    /**
     * 检查大屏名称是否重复
     */
    @PostMapping("/checkVisualBaseNameUnique")
    @ResponseBody
    public Boolean checkVisualBaseNameUnique(@Validated String id,@Validated String visualName){
        return userVisualBaseService.checkVisualBaseNameUnique(visualName,id) <= 0;
    }

    /**
     * 查看大屏信息json
     */
    @GetMapping("/showCssJson/{id}")
    public String showCssJson(@PathVariable("id") String id, ModelMap map)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        map.put("user",user);
        map.put("cssStyle", userVisualBaseService.selectDataJsonByBaseId(id));
        return prefix + "/showCssJson";
    }
}
